#include "I2CInterface.h"
int LED_I2C::fd; 
LED_I2C::LED_I2C( uint device_addr_in )
{
  device_addr = device_addr_in;
}

LED_I2C::~LED_I2C()
{
  if (fd >= 0)  close(fd);
      
}

int LED_I2C:: LED_I2C_open(const char* dev)
{       
         
  fd = open( dev,O_RDWR);
  if(fd < 0){
  //  printf("i2c device open failed!\n");
    return false;
  }
  return true;
}
 
int LED_I2C:: LED_I2C_set (  uint timeout, uint retry )
{
  if (fd == 0)
  return -1;

  //  timeout = I2C_DEFAULT_TIMEOUT ;
  //  retry   = I2C_DEFAULT_RETRY   ;
  
  ioctl(fd, I2C_TIMEOUT, timeout);
  ioctl(fd, I2C_RETRIES, retry  );
  return 0;
}

int LED_I2C:: LED_I2C_send( struct i2c_rdwr_ioctl_data *data )
{
  
  if (fd < 0)
    return -1;

  if (data == NULL)
    return -1;

  if (data->msgs == NULL || data->nmsgs == 0)
    return -1;
  
  return ioctl(fd, I2C_RDWR, (unsigned long)data) ;
}

int LED_I2C:: LED_I2C_writedata( uint rdwr_addr, uint val )
{
  int ret = 0;

  struct i2c_rdwr_ioctl_data *data;

  if ((data = (struct i2c_rdwr_ioctl_data *)malloc(sizeof(struct i2c_rdwr_ioctl_data))) == NULL)
    return -1;

  data->nmsgs = 1;
  
  if ((data->msgs = (struct i2c_msg *)malloc(data->nmsgs * sizeof(struct i2c_msg))) == NULL) {
    ret = -1;
    goto errexit2;
  }
  if ((data->msgs[0].buf = (unsigned char *)malloc(2 * sizeof(unsigned char))) == NULL) {
    ret = -1;
    goto errexit1;
  }

  data->msgs[0].addr = device_addr;
  data->msgs[0].flags = 0;
  data->msgs[0].len = 2;
  data->msgs[0].buf[0] =rdwr_addr;
  data->msgs[0].buf[1] = val;

  if ((ret = LED_I2C_send( data)) < 0)
    goto errexit0;
  free(data->msgs[0].buf);
  free(data->msgs);
  free(data);
errexit0:
  free(data->msgs[0].buf);
errexit1:
  free(data->msgs);
errexit2:
  free(data);


  return ret;
}

int LED_I2C:: LED_I2C_readdata( uint rdwr_addr,uchar *buffer)
{
  int i,ret = 0;

    struct i2c_rdwr_ioctl_data *data;

  if ((data = (struct i2c_rdwr_ioctl_data *)malloc(sizeof(struct i2c_rdwr_ioctl_data))) == NULL)
  return -1;

  data->nmsgs = 2;
  if ((data->msgs = (struct i2c_msg *)malloc(data->nmsgs * sizeof(struct i2c_msg))) == NULL) {
    ret = -1;
    goto errexit3;
  }
  if ((data->msgs[0].buf = (unsigned char *)malloc(sizeof(unsigned char))) == NULL) {
    ret = -1;
    goto errexit2;
  }
  if ((data->msgs[1].buf = (unsigned char *)malloc(sizeof(unsigned char))) == NULL) {
    ret = -1;
    goto errexit1;
  }

  data->msgs[0].addr = device_addr;
  data->msgs[0].flags = 0;
  data->msgs[0].len = 1;
  data->msgs[0].buf[0] = rdwr_addr;

  data->msgs[1].addr = device_addr;
  data->msgs[1].flags = I2C_M_RD;
  data->msgs[1].len = 13;      //original data is 1
  data->msgs[1].buf[0] = 0;

  if ((ret =  LED_I2C_send( data)) < 0)
    goto errexit0;

  for(i = 0 ;i < data->msgs[1].len; i++)
    buffer[i] = data->msgs[1].buf[i];
  free(data->msgs[1].buf);
  free(data->msgs[0].buf);
  free(data->msgs);
  free(data);
errexit0:
  free(data->msgs[1].buf);
errexit1:
  free(data->msgs[0].buf);
errexit2:
  free(data->msgs);
errexit3:
  free(data);


  return ret;
}

//void LED_I2C::LED_I2C_close(int fd)
//{
//  if (fd < 0)
//    return;
//
//  close(fd);
//}  
  

